package com.googlecode.bipol.war.services;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.googlecode.bip.dao.DAOFactory;
import com.googlecode.bip.dao.QuestionDAO;
import com.googlecode.bip.entity.IQuestion;

/**
 *
 *
 */
public class QuestionManagerImpl implements QuestionManager, Serializable {
    private static final long serialVersionUID = 1L;

    @Override
    public IQuestion createQuestion(int topicId, String name,
            String description, String idLocal, int minValue, int maxValue,
            int defaultValue) throws ServiceException {

        // Check that topic exists
        ServicesHelper.getExistingTopic(topicId, idLocal);

        // Insert the new question
        try {
            IQuestion q = DAOFactory.getDAOFactory().getQuestionDAO().insertQuestion(topicId,minValue,maxValue,defaultValue,idLocal,name,description);
            if(q != null) {
                return q;
                }
        } catch(Exception e) {
            throw new ServiceException("Error when saving question: "+e.getMessage(), e);
        }
        throw new ServiceException("Error when saving question: Unknown reason (please contact the administrator)");
    }

    public IQuestion postQuestion(IQuestion question) throws ServiceException {
        // Check that topic exists
        ServicesHelper.getExistingTopic(question.getParentIdTopic(), question.getIdLocal());
        if (question.getIdQuestion() > 0) {
            // Update question
            try {
                if (DAOFactory.getDAOFactory().getQuestionDAO().updateQuestion(question)) {
                    // Update question description's values
                    for (List<Object> o: question.getListTextValues()) {
                        if (o.get(1) != null) {
                            DAOFactory.getDAOFactory().getQuestionDAO().updateQuestionValueDescription(
                                    question, Integer.class.cast(o.get(0)), String.class.cast(o.get(1)));
                        }
                    }
                    return question;
                }
            } catch(Exception e) {
                throw new ServiceException("Error when updating question: "+e.getMessage(), e);
            }
            throw new ServiceException("Error when updating question: Unknown reason (please contact the administrator)");
        }
        else {
            // Insert the new question
            try {
                IQuestion q = DAOFactory.getDAOFactory().getQuestionDAO().insertQuestion(
                        question.getParentIdTopic(),
                        question.getMinValue(),
                        question.getMaxValue(),
                        question.getDefaultValue(),
                        question.getIdLocal(),
                        question.getName(),
                        question.getDescription());
                if(q != null) {
                    return q;
                } else {
                    throw new ServiceException("Error when creating question: Unknown reason (please contact the administrator)");
                }
            } catch(Exception e) {
                throw new ServiceException("Error when creating question: "+e.getMessage(), e);
            }
        }
    }


    @Override
    public IQuestion removeQuestion(int idquestion, String idlocal) throws ServiceException {
        // Check that question exists
        IQuestion q = ServicesHelper.getExistingQuestion(idquestion, idlocal);
        // Delete question
        try {
            QuestionDAO dao = DAOFactory.getDAOFactory().getQuestionDAO();

            dao.deleteQuestionLocal(idquestion, idlocal);

            // REMOVE QUESTION for all languages
            //dao.deleteQuestionLocals(idquestion));
            //dao.deleteQuestion(idquestion);

            return q; // FIXME: Well, q does not exist any more (for this language)
            }
        catch(Exception e) {
            throw new ServiceException("Error when deleting question: "+e.getMessage(), e);
        }
    }

    @Override
    public List<IQuestion> getQuestionsOfTopic(int idtopic, String idlocal) throws ServiceException {
        // Check that topic exists
        ServicesHelper.getExistingTopic(idtopic, idlocal);
        // Get related questions
        try {
            return DAOFactory.getDAOFactory().getQuestionDAO().findQuestions(idtopic,idlocal);
        } catch(Exception e) {
            throw new ServiceException("Error when getting questions of topic with id \'"+idtopic+"\' and local \'"+idlocal+"\': "+e.getMessage(), e);
        }
    }

    @Override
    @Deprecated
    public Map<Integer,String> fetchQuestionTextValues(int idquestion, String idlocal) throws ServiceException {
        // Check that question exists
        IQuestion q = ServicesHelper.getExistingQuestion(idquestion, idlocal);
        Map<Integer,String> res = new HashMap<Integer,String>();
        try {
            for (int i=q.getMinValue();i<=q.getMaxValue();i++) {
                res.put(i, q.getTextValue(i));
            }
        } catch(Exception e) {
            throw new ServiceException("Error when getting question text values\': "+e.getMessage(), e);
        }

        return res;
    }


}
